package com.bookm.bookm.controller;

import cn.hutool.db.Db;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.bookm.bookm.entity.Result;
import com.bookm.bookm.service.BookingService;
import com.bookm.bookm.service.impl.BookingServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/booking")
public class BookingServlet extends HttpServlet {
    BookingServiceImpl service = new BookingServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String key = req.getParameter("key");
        String bookId = req.getParameter("bookId");
        String id = req.getParameter("id");
        PrintWriter out = resp.getWriter();
        switch (key){
            case "getBookingList":
                getList(req, out);
                break;
            case "getOneBooking":
                String searchText=req.getParameter("searchText");
                getOne(req,out,searchText);
                break;
            case "deleteBooking":
                deleteBooking(req,out,id,bookId);
                break;
            case "addBooking":
                addBooking(req,out,id,bookId);
                break;
            case "getReaderBookingList":
                getReaderBookingList(req,out,id);
                break;
        }
    }

    public void getList(HttpServletRequest req,PrintWriter out){
        Result result = service.getBookingList();
        out.println(result.toString());
    }

    public void getOne(HttpServletRequest req,PrintWriter out,String searchText){
        Result result = service.getOneBooking(searchText);
        out.println(result);
    }

    public void deleteBooking(HttpServletRequest req,PrintWriter out,String id,String bookId){
        Result result = service.deleteBooking(id,bookId);
        out.println(result);
    }

    public void addBooking(HttpServletRequest req,PrintWriter out,String id,String bookId){
        Result result = service.addBooking(id,bookId);
        out.println(result);
    }

    public void getReaderBookingList(HttpServletRequest req,PrintWriter out,String id){
        Result result = service.getReaderBookingList(id);
        out.println(result);
    }
}
